﻿using System;
using System.Text;
using System.IO;
using System.Web;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text.RegularExpressions ;
using System.Web.UI.WebControls.WebParts;
using System.Reflection;


using Com.FastSearch.SharePoint.WebControls;
//using Com.FastSearch.Esp.Search.Result;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI
{
    //public class SliderBreadCrumb : SearchBase
    public class ESPWeightedSlider : WebPart
    {
        StreamReader sr;
        string SilverLightCode;
        private string wpWidth = "800";     //Default width of the control in pixels
        private string wpHeight = "150";    //Default height of the control in pixels

        HttpRequest myRequest = HttpContext.Current.Request; 

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
           
            StringBuilder sbOut = new StringBuilder();
            string wtEspNavigator = string.Empty;

            try
            {
                if (!string.IsNullOrEmpty(Page.Request.QueryString["XRANK"]))
                {
                    wtEspNavigator = Page.Request.QueryString["XRANK"];
                }

                if (base.Width.Value > 0.0)
                {
                    wpWidth = base.Width.Value.ToString();
                }

                sbOut.AppendLine("<DIV ID=\"DivHdnWeightedQuery\" style=\"display:none\" >");  //style=\"display:none\"
                sbOut.AppendLine("</DIV>");
                sbOut.AppendLine("<input type=\"hidden\" ID=\"WtEspNavigator\" name=\"WtEspNavigator\" value=\"" + wtEspNavigator + "\">");
                //sbOut.AppendLine("<input type=\"text\" ID=\"TestBox\" name=\"TestBox\" >");

                Assembly assembly = Assembly.GetExecutingAssembly();
                ////<script type="text/javascript" src="/wpresources/js/Silverlight.js"></script>
                //Stream stream = assembly.GetManifestResourceStream("Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.src.js.Silverlight.js");
                //sr = new StreamReader(stream);
                //sbOut.Append(sr.ReadToEnd());


                Stream stream = assembly.GetManifestResourceStream("Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.src.js.SliderCode.txt");
                sr = new StreamReader(stream);
                SilverLightCode = sr.ReadToEnd();
                sr.Close();

                SilverLightCode = SilverLightCode.Replace("XXXWIDTHXXX", (Convert.ToDouble(wpWidth) - 3).ToString());
                SilverLightCode = SilverLightCode.Replace("XXXUSESLIDERXXX", _useSlider.ToString() );
                SilverLightCode = SilverLightCode.Replace("XXXSLIDERLOWXXX", _sliderLow.ToString());
                SilverLightCode = SilverLightCode.Replace("XXXSLIDERHIGHXXX", _sliderHigh.ToString());
                SilverLightCode = SilverLightCode.Replace("XXXSLIDERBKGXXX", _sliderBackground);
                SilverLightCode = SilverLightCode.Replace("XXXSLIDERFONTXXX", _sliderFont);
                SilverLightCode = SilverLightCode.Replace("XXXDATAXXXPLACEHOLDERXXX", "'" + wtEspNavigator+ "'");
                
                sbOut.Append(SilverLightCode); ;
            }
            
            catch (Exception e)
            {
                writer.Write(e.Message + " " + e.StackTrace);

            }
            finally
            {
                writer.Write(sbOut.ToString());
            }
        }

        #region WebPart Properties

        private bool _useSlider = false;     // _useSlider == true -> use slider for each element, false -> use drag for each element
        //[Category("Search configuration"), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Use Slider for assigning weights"), WebDescription("Use Slider/drag feature to assign weights")]
        //public bool UseSlider
        //{
        //    get { return _useSlider; }
        //    set { _useSlider = value; }
        //}

        private int _sliderLow = 100;      
        //[Category("Search configuration"), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Lowest Value of Weights"), WebDescription("Lowest Value of Weights Assigned")]
        //public int SliderLow
        //{
        //    get { return _sliderLow; }
        //    set { _sliderLow = value; }
        //}

        private int _sliderHigh = 1000;
        //[Category("Search configuration"), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Highest Value of Weights"), WebDescription("Highest Value of Weights Assigned")]
        //public int SliderHigh
        //{
        //    get { return _sliderHigh; }
        //    set { _sliderHigh = value; }
        //}
        private string _sliderBackground = "Transparent";
        [Category("Search configuration"), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Slider Background Color"), WebDescription("Slider Background Color")]
        public string SliderBackground
        {
            get { return _sliderBackground; }
            set { _sliderBackground = value; }
        }
        private string _sliderFont = "FontSize=\"12\" FontFamily=\"Verdana\" FontStyle=\"Normal\" FontWeight=\"Normal\"";
        [Category("Search configuration"), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Slider Font Style"), WebDescription("Slider Font Style")]
        public string SliderFont
        {
            get { return _sliderFont; }
            set { _sliderFont = value; }
        }
         
        #endregion
    }
}
